<?php
/*
Simple:Press Forum
Public - global code
$LastChangedDate: 2010-02-24 22:04:50 -0700 (Wed, 24 Feb 2010) $
$Rev: 3596 $
*/

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
	echo (__('Access Denied', "sforum"));
	die();
}

# ------------------------------------------------------------------
# sf_mail_filter_from()
#
# Filter Call
# Sets up the 'from' email options
#	$from:		Passed in to filter
# ------------------------------------------------------------------
function sf_mail_filter_from($from)
{
	$sfmail = sf_get_option('sfmail');
	$mailfrom = $sfmail['sfmailfrom'];
	$maildomain = $sfmail['sfmaildomain'];
	if((!empty($mailfrom)) && (!empty($maildomain)))
	{
		$from = $mailfrom.'@'.$maildomain;
	}
	return $from;
}

# ------------------------------------------------------------------
# sf_mail_filter_name()
#
# Filter Call
# Sets up the 'from' email options
#	$from:		Passed in to filter
# ------------------------------------------------------------------
function sf_mail_filter_name($from)
{
	$sfmail = sf_get_option('sfmail');
	$mailsender = $sfmail['sfmailsender'];
	if(!empty($mailsender))
	{
		$from = $mailsender;
	}
	return $from;
}

# ------------------------------------------------------------------
# sf_block_admin()
#
# Blocks normal users from accessing WP admin area
# ------------------------------------------------------------------
function sf_block_admin()
{
	global $current_user, $wp_roles;

	# Is this the admin interface?
	if (strstr(strtolower($_SERVER['REQUEST_URI']),'/wp-admin/') && !strstr(strtolower($_SERVER['REQUEST_URI']),'async-upload.php'))
	{
		# get the user level and required level to access admin pages
		$sfblock = array();
		$sfblock = sf_get_option('sfblockadmin');
		if ($sfblock['blockadmin'])
		{
	        if (isset($sfblock['blockrole']))
	        {
	            $minrole = get_role($sfblock['blockrole']);
	            if (isset($minrole->capabilities))
	            {
	                $req_level = array_reduce( array_keys( $minrole->capabilities ), array( &$current_user, 'level_reduction' ), 0 );
	             } else {
	                $req_level = 10;
	             }
	        } else {
	            $req_level = 10;
	        }
	        $user_level = array_reduce( array_keys( $current_user->allcaps ), array( &$current_user, 'level_reduction' ), 0 );

			# block admin if required
			$is_moderator = sf_get_member_item($current_user->ID, 'moderator');
			if (!sfc_current_user_can('SPF Manage Options') &&
			    !sfc_current_user_can('SPF Manage Forums') &&
			    !sfc_current_user_can('SPF Manage Components') &&
			    !sfc_current_user_can('SPF Manage User Groups') &&
			    !sfc_current_user_can('SPF Manage Permissions') &&
			    !sfc_current_user_can('SPF Manage Tags') &&
			    !sfc_current_user_can('SPF Manage Users') &&
			    !sfc_current_user_can('SPF Manage Profiles') &&
			    !sfc_current_user_can('SPF Manage Admins') &&
			    !sfc_current_user_can('SPF Manage Toolbox') &&
			    !sfc_current_user_can('SPF Manage Configuration') &&
				!$is_moderator &&
				$user_level < $req_level
			    )
			{
				if ($sfblock['blockprofile'])
				{
					$redirect = SFURL.'profile/';
				} else {
					$redirect = $sfblock['blockredirect'];
				}
				wp_redirect($redirect, 302);
			}
		}
	}
}

#
# Create canonical URL for AIOSEO
# ------------------------------------------------------------------
function sf_aioseo_canonical_url($url)
{
	global $sfvars, $sfglobals, $ISFORUM;

	if ($ISFORUM)
	{
		sf_populate_query_vars();
		$sfglobals['canonicalurl'] = true;
		$url = sf_build_url($sfvars['forumslug'], $sfvars['topicslug'], $sfvars['page'], 0);
	}
	return $url;
}

#
# Create meta description for AIOSEO
# ------------------------------------------------------------------
function sf_aioseo_description($aioseo_descr)
{
	global $sfglobals, $ISFORUM;

	if ($ISFORUM)
	{
		sf_populate_query_vars();
		sf_setup_page_type();
		$sfglobals['metadescription'] = true;

		$description = sfc_get_metadescription();
		if ($description != '')
		{
			$aioseo_descr = $description;
		}
	}

	return $aioseo_descr;
}

#
# Create meta keywords for AIOSEO
# ------------------------------------------------------------------
function sf_aioseo_keywords($aioseo_keywords)
{
	global $sfglobals, $ISFORUM;

	if ($ISFORUM)
	{
		sf_populate_query_vars();
		sf_setup_page_type();
		$sfglobals['metakeywords'] = true;
		$keywords = sfc_get_metakeywords();
		if ($keywords != '')
		{
			$aioseo_keywords = $keywords;
		}
	}

	return $aioseo_keywords;
}

# ------------------------------------------------------------------
function sf_aioseo_homepage($title)
{
	global $ISFORUM;

	if ($ISFORUM)
	{
		$sfseo = array();
		$sfseo = sf_get_option('sfseo');
		$title = sf_setup_title($title, ' '.$sfseo['sfseo_sep'].' ');
	}
	return $title;
}

function sf_build_sitemap()
{
	global $wpdb;

	$generatorObject = &GoogleSitemapGenerator::GetInstance();
	if ($generatorObject != null)
	{
		$topics = $wpdb->get_results("SELECT topic_id, topic_slug, forum_slug FROM ".SFTOPICS." JOIN ".SFFORUMS." ON ".SFTOPICS.".forum_id = ".SFFORUMS.".forum_id WHERE in_sitemap = 1 ORDER BY ".SFTOPICS.".post_ID DESC");
		if ($topics)
		{
			foreach ($topics as $topic)
			{
				$url = sf_build_url($topic->forum_slug, $topic->topic_slug, 0, 0);
				$time = $wpdb->get_var("SELECT UNIX_TIMESTAMP(post_date) as udate FROM ".SFPOSTS." WHERE topic_id = ".$topic->topic_id." ORDER BY post_id DESC LIMIT 1");
				$generatorObject->AddUrl($url, $time, "daily", 0.5);
			}
		}
	}
}

?>